<?php

class admin_Model_AdministradorMapper {

    protected $_dbTable;

    public function setDbTable($dbTable) {

        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }

        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Enlace a tabla de datos inválido');
        }

        $this->_dbTable = $dbTable;

        return $this;
    }

    public function getDbTable() {

        if (null === $this->_dbTable) {
            $this->setDbTable('admin_Model_DbTable_Administrador');
        }

        return $this->_dbTable;
    }


    public function obtenerTodos()
    {
        return $this->getDbTable()->fetchAll();
    }

    public function obtenerPorId($id)
    {
        $id = (int) $id;
        return $this->getDbTable()->find($id)->current();

    }

    public function obtenerPorNombre($nombre)
    {

        $select = $this->getDbTable()->select();
        $select->where('nombre=?',$nombre);
        return $this->getDbTable()->fetchAll($select);

    }

    public function obtenerCuenta($correo,$clave){

        $select = $this->getDbTable()->select();
        $select->where('correo=?',$correo);
        $select->where('clave=?',$clave);
        return $this->getDbTable()->fetchAll($select);


    }

    public function agregar(admin_Model_Administrador $objAdministrador){

        $administrador = $this->getDbTable()->createRow();
        $administrador->nombre = $objAdministrador->getNombre();
        $administrador->apellido = $objAdministrador->getApellido();
        $administrador->correo = $objAdministrador->getCorreo();
        $administrador->clave  = md5($objAdministrador->getClave());
        $administrador->nivel  = $objAdministrador->getNivel();
        $administrador->save();

    }

    public function editar(admin_Model_Administrador $objAdministrador){

        $administrador = array(
            'nombre'  => $objAdministrador->getNombre(),
            'apellido'=> $objAdministrador->getApellido(),
            'correo'  => $objAdministrador->getCorreo(),
            'clave'   => md5($objAdministrador->getClave()),
            'nivel'   => $objAdministrador->getNivel()
        );

        $this->getDbTable()->update($administrador,'id='.(int)$objAdministrador->getId());

    }

    public function eliminar($id){

        $id = (int) $id;
        $administrador = $this->getDbTable()->find($id)->current();

        if($administrador !== null){
            $administrador->delete();
        }

    }
    



}

